const router={
	render:function(list,id) {
	 //匹配当前的路由,匹配不到则使用404配置内容 并渲染~
	  var appid=id||'#page'
	  var state=this.getState()
	  var ELEMENT = document.querySelector(appid);
	  var exitElement=''
	  list.forEach(function(ele){
		  if(ele.path === state || ele.name === state) exitElement = ele
	  })
	  if(exitElement) {
		var path= this.renderPath(state)
	    document.title = exitElement.title || 'hash前端路由';
		$(appid).load(path);
	  } else{
		// ELEMENT.innerText ='页面不存在';
		$(appid).load('./src/error/404.html');
	  }
	},
	renderPath:function(state){
		state=state==='/' ? 'index':state
		var dir='./src/'
		return dir+state+'.html'
	},
	push:function(path) {
		window.location.hash = path;
	},
	//使用location.replace实现替换 功能 
	replace:function(path) {
		window.location.replace(this.getUrl(path));
	},
	//这里使用history模式的go方法进行模拟 前进/后退 功能
	go:function(n) {
		window.history.go(n);
	},
	getState:function() {
		const hash = window.location.hash;
		if(hash) {
			var pageUrl=hash.slice(1)
			return pageUrl.split('?')[0]
		}
		console.log(pageUrl)
		return '/'
	},
	getQuery:function(name) {
		var after = window.location.hash.split('?')[1]
		if (after) {
			const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
			const r = after.match(reg)
			if (r != null) {
			  return decodeURIComponent(r[2])
			} else {
			  return null
			}
		} else {
			const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
			const r = window.location.search.substr(1).match(reg)
			// console.log(r)

			if (r != null) {
			  return unescape(r[2])
			}
			return null
		}
	},
	//获取完整url
	getUrl:function(path) {
		const href = window.location.href;
		const i = href.indexOf('#');
		const base = i >= 0 ? href.slice(0, i) : href;
		return `${base}#${path}`;
	}
}